﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;

namespace System.Windows.Media.DirectShow.Filters
{
    internal class AudioVisualizationOutputPin : BaseOutputPin
    {

        private WaveFormatExtensible format;
        private AudioVisualization filter;

        private AMMediaType[] _mediaTypes = new AMMediaType[] { new AMMediaType {
            MajorType = MediaMajorTypes.Audio,
            SubType = MediaSubTypes.PCM,
        }};

        public AudioVisualizationOutputPin(AudioVisualization filter) : base(filter)
        {
            this.filter = filter;
        }

        public override void ProcessSample(IMediaSample sample)
        {
            // Auto set format during sample processing
            if (format == null)
            {
                format = sample.MediaType.Format as WaveFormatExtensible;
            }

            base.ProcessSample(sample);
        }
        
        protected internal override void DeliverAsync(IMediaSample sample)
        {
            TimeSpan start = TimeSpan.FromSeconds(sample.StartMediaTime / (double)format.AverageBytesPerSecond);
            TimeSpan end = TimeSpan.FromSeconds(sample.EndMediaTime / (double)format.AverageBytesPerSecond);

            // Set sample parameters
            sample.StartTime = start;
            sample.EndTime = end;
            sample.MediaType = this._mediaTypes[0];

            base.DeliverAsync(sample);
        }

        public override string Name
        {
            get { return "FFT output pin"; }
        }

        public override System.Collections.Generic.IEnumerable<AMMediaType> MediaTypes
        {
            get { return _mediaTypes; }
        }
    }
}
